package com.sgb.member.ui.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.member.MemberConfig;
import com.sgb.member.R;
import com.sgb.member.databinding.ActivityMemberLeverSettingBinding;
import com.sgb.member.entity.MemberLeverBean;
import com.sgb.member.ui.adapter.MemberLeverSettingAdapter;
import com.sgb.member.view_model.MemberLeverViewModel;
import com.sgb.member.weight.SimpleResult;
import com.swgk.core.base.AppActivity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

public class MemberLeverSettingActivity extends AppActivity {
    private MemberLeverViewModel sModel;
    private ActivityMemberLeverSettingBinding mBinding;
    private MemberLeverSettingAdapter mAdapter;


    public static void start(Context context) {
        Intent intent = new Intent(context, MemberLeverSettingActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_member_lever_setting);
        sModel = new ViewModelProvider(this).get(MemberLeverViewModel.class);
        mBinding.setLifecycleOwner(this);
        mBinding.activityBar.titleView.setText("标准设定");
        mBinding.activityBar.ivBack.setOnClickListener(v -> finish());
        registerObserver();
        initRecyclerView();

        sModel.getRatLeverList();
    }

    private void initRecyclerView() {
        mBinding.recyclerView.setLoadingMoreEnabled(false);
        mBinding.recyclerView.setPullRefreshEnabled(true);
        mBinding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                sModel.getRatLeverList();
            }

            @Override
            public void onLoadMore() {

            }
        });
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new MemberLeverSettingAdapter(this, null) {
            @Override
            protected void onEditClick(int position, MemberLeverBean leverBean) {
                /*new XPopup.Builder(MemberLeverSettingActivity.this)
                        .asCustom(new MemberLeverRatPopup(getActivity(), leverBean, new SimpleResult<MemberLeverBean>() {
                            @Override
                            public void onResult(MemberLeverBean bean, Object... objects) {
                                mUpdateBean = bean;
                                sModel.updateRatLeverList(bean);
                            }
                        })).show();*/
                LeverSettingEditActivity.start(MemberLeverSettingActivity.this, leverBean);
            }

            @Override
            protected void onLookClick(int position, MemberLeverBean bean) {
                sModel.getH5Url(bean.getId(), new SimpleResult<String>() {
                    @Override
                    public void onResult(String s, Object... objects) {
                        MemberConfig.getInstance().startWebViewX5(getActivity(), s);
                    }
                });
            }
        };
        mBinding.recyclerView.setAdapter(mAdapter);
    }

    private void registerObserver() {
        sModel.dialog.observe(this, aBoolean -> {
            if (aBoolean) {
                if (!DialogHelper.isShowing()) {
                    DialogHelper.showProgressDialog(this, "数据获取中...");
                }
            } else {
                DialogHelper.dismissProgressDialog();
                mBinding.recyclerView.refreshComplete();
            }
        });
        sModel.empty.observe(this, aBoolean -> {
            if (aBoolean) {
                mAdapter.clearDatas();
                mBinding.emptyView.setVisibility(View.VISIBLE);
            } else {
                mBinding.emptyView.setVisibility(View.GONE);
            }
        });
        sModel.mLeverData.observe(this, new Observer<List<MemberLeverBean>>() {
            @Override
            public void onChanged(List<MemberLeverBean> memberLeverBeans) {
                if (memberLeverBeans != null && memberLeverBeans.size() > 0) {
                    mAdapter.setDatas(memberLeverBeans);
                } else {
                    mAdapter.clearDatas();
                    mBinding.emptyView.setVisibility(View.VISIBLE);
                }
            }
        });
     /*   sModel.mLeverUpdate.observe(this, new Observer<BaseEntity<Object>>() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onChanged(BaseEntity<Object> objectBaseEntity) {
                int index = mAdapter.getDatas().indexOf(mUpdateBean);
                if (index > 0) {
                    mAdapter.getDatas().set(index, mUpdateBean);
                }
                mAdapter.notifyDataSetChanged();
            }
        });*/
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == MemberConfig.REQ_SERVICE) {
            MemberLeverBean leverBean = (MemberLeverBean) data.getSerializableExtra(MemberConfig.EXTRA_ENTITY);
            int indexOf = mAdapter.getDatas().indexOf(leverBean);
            if(indexOf != -1){
                mAdapter.getDatas().set(indexOf, leverBean);
                mAdapter.notifyDataSetChanged();
            } else {
                sModel.getRatLeverList();
            }
        }
    }
}
